package com.tc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tc.dao.OrderItemMapper;
import com.tc.pojo.OrderItem;
import com.tc.service.OrderItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:admin
 * @Description:OrderItem业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class OrderItemServiceImpl implements OrderItemService {

    @Autowired
    private OrderItemMapper orderItemMapper;


    /**
     * OrderItem条件+分页查询
     * @param orderItem 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<OrderItem> findPage(OrderItem orderItem, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(orderItem);
        //执行搜索
        return new PageInfo<OrderItem>(orderItemMapper.selectByExample(example));
    }

    /**
     * OrderItem分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<OrderItem> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<OrderItem>(orderItemMapper.selectAll());
    }

    /**
     * OrderItem条件查询
     * @param orderItem
     * @return
     */
    @Override
    public List<OrderItem> findList(OrderItem orderItem){
        //构建查询条件
        Example example = createExample(orderItem);
        //根据构建的条件查询数据
        return orderItemMapper.selectByExample(example);
    }


    /**
     * OrderItem构建查询对象
     * @param orderItem
     * @return
     */
    public Example createExample(OrderItem orderItem){
        Example example=new Example(OrderItem.class);
        Example.Criteria criteria = example.createCriteria();
        if(orderItem!=null){
            // 订单id
            if(!StringUtils.isEmpty(orderItem.getId())){
                    criteria.andEqualTo("id",orderItem.getId());
            }
            // 订单编号
            if(!StringUtils.isEmpty(orderItem.getOrderSn())){
                    criteria.andEqualTo("orderSn",orderItem.getOrderSn());
            }
            // 提交时间
            if(!StringUtils.isEmpty(orderItem.getCreateTime())){
                    criteria.andEqualTo("createTime",orderItem.getCreateTime());
            }
            // 订单总金额
            if(!StringUtils.isEmpty(orderItem.getTotalAmount())){
                    criteria.andEqualTo("totalAmount",orderItem.getTotalAmount());
            }
            // 应付金额（实际支付金额）有优惠券功能考虑
            if(!StringUtils.isEmpty(orderItem.getPayAmount())){
                    criteria.andEqualTo("payAmount",orderItem.getPayAmount());
            }
            // 支付方式：0->未支付；1->支付宝；2->微信
            if(!StringUtils.isEmpty(orderItem.getPayType())){
                    criteria.andEqualTo("payType",orderItem.getPayType());
            }
            // 完成订单可以获得的积分
            if(!StringUtils.isEmpty(orderItem.getIntegration())){
                    criteria.andEqualTo("integration",orderItem.getIntegration());
            }
            // 可以获得的成长值
            if(!StringUtils.isEmpty(orderItem.getGrowth())){
                    criteria.andEqualTo("growth",orderItem.getGrowth());
            }
            // 使用优惠券类型
            if(!StringUtils.isEmpty(orderItem.getDiscounts())){
                    criteria.andEqualTo("discounts",orderItem.getDiscounts());
            }
            // 发票类型：0->不开发票；1->电子发票；2->纸质发票
            if(!StringUtils.isEmpty(orderItem.getBillType())){
                    criteria.andEqualTo("billType",orderItem.getBillType());
            }
            // 发票抬头
            if(!StringUtils.isEmpty(orderItem.getBillHeader())){
                    criteria.andEqualTo("billHeader",orderItem.getBillHeader());
            }
            // 发票内容
            if(!StringUtils.isEmpty(orderItem.getBillContent())){
                    criteria.andEqualTo("billContent",orderItem.getBillContent());
            }
            // 收票人电话
            if(!StringUtils.isEmpty(orderItem.getBillReceiverPhone())){
                    criteria.andEqualTo("billReceiverPhone",orderItem.getBillReceiverPhone());
            }
            // 收票人邮箱
            if(!StringUtils.isEmpty(orderItem.getBillReceiverEmail())){
                    criteria.andEqualTo("billReceiverEmail",orderItem.getBillReceiverEmail());
            }
            // 省份/直辖市
            if(!StringUtils.isEmpty(orderItem.getReceiverProvince())){
                    criteria.andEqualTo("receiverProvince",orderItem.getReceiverProvince());
            }
            // 城市
            if(!StringUtils.isEmpty(orderItem.getReceiverCity())){
                    criteria.andEqualTo("receiverCity",orderItem.getReceiverCity());
            }
            // 区
            if(!StringUtils.isEmpty(orderItem.getReceiverRegion())){
                    criteria.andEqualTo("receiverRegion",orderItem.getReceiverRegion());
            }
            // 详细地址
            if(!StringUtils.isEmpty(orderItem.getReceiverDetailAddress())){
                    criteria.andEqualTo("receiverDetailAddress",orderItem.getReceiverDetailAddress());
            }
            // 订单评价
            if(!StringUtils.isEmpty(orderItem.getComment())){
                    criteria.andEqualTo("comment",orderItem.getComment());
            }
            // 下单时使用的积分
            if(!StringUtils.isEmpty(orderItem.getUseIntegration())){
                    criteria.andEqualTo("useIntegration",orderItem.getUseIntegration());
            }
            // 开始运送时间
            if(!StringUtils.isEmpty(orderItem.getStartmentTime())){
                    criteria.andEqualTo("startmentTime",orderItem.getStartmentTime());
            }
            // 结束行程的时间
            if(!StringUtils.isEmpty(orderItem.getEndmentTime())){
                    criteria.andEqualTo("endmentTime",orderItem.getEndmentTime());
            }
            // 支付时间
            if(!StringUtils.isEmpty(orderItem.getPaymentTime())){
                    criteria.andEqualTo("paymentTime",orderItem.getPaymentTime());
            }
            // 出发时间
            if(!StringUtils.isEmpty(orderItem.getDeliveryTime())){
                    criteria.andEqualTo("deliveryTime",orderItem.getDeliveryTime());
            }
            // 评价时间
            if(!StringUtils.isEmpty(orderItem.getCommentTime())){
                    criteria.andEqualTo("commentTime",orderItem.getCommentTime());
            }
            // 修改时间(对订单做过修改)
            if(!StringUtils.isEmpty(orderItem.getModifyTime())){
                    criteria.andEqualTo("modifyTime",orderItem.getModifyTime());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Long id){
        orderItemMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改OrderItem
     * @param orderItem
     */
    @Override
    public void update(OrderItem orderItem){
        orderItemMapper.updateByPrimaryKey(orderItem);
    }

    /**
     * 增加OrderItem
     * @param orderItem
     */
    @Override
    public void add(OrderItem orderItem){
        orderItemMapper.insert(orderItem);
    }

    /**
     * 根据ID查询OrderItem
     * @param id
     * @return
     */
    @Override
    public OrderItem findById(Long id){
        return  orderItemMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询OrderItem全部数据
     * @return
     */
    @Override
    public List<OrderItem> findAll() {
        return orderItemMapper.selectAll();
    }
}
